package com.wuzuqing.android.eightdiagrams

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.CheckBox
import android.widget.EditText
import android.widget.RadioGroup
import android.widget.Spinner
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wuzuqing.android.eightdiagrams.base.BaseAdapter
import com.wuzuqing.android.eightdiagrams.model.City
import com.wuzuqing.android.eightdiagrams.model.MeasureParams
import com.wuzuqing.android.eightdiagrams.model.MeasureType
import com.wuzuqing.android.eightdiagrams.utils.DataUtils
import com.wuzuqing.android.eightdiagrams.utils.EightDagramsManager
import com.wuzuqing.android.eightdiagrams.utils.LunarUtil

class MeiHuaYiShuActivity : AppCompatActivity() {

    lateinit var top: EditText
    lateinit var bottom: EditText
    lateinit var mEtText: EditText
    lateinit var ganZhi: TextView
    lateinit var mCbUseTime: CheckBox
    lateinit var mNumberContainer: View
    lateinit var mTextContainer: View
    lateinit var mSelectContainer: View
    lateinit var upSpinner: Spinner
    lateinit var bottomSpinner: Spinner
    lateinit var yaoSpinner: Spinner

    var mMeasureType: MeasureType = MeasureType.NUMBER
    var mUpIndex = 0
    var mBottomIndex = 0
    var mYaoIndex = 0


    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_meihuayishu)


        top = findViewById(R.id.top)
        bottom = findViewById(R.id.bottom)
        mEtText = findViewById(R.id.text)
        ganZhi = findViewById(R.id.ganzhi)
        mCbUseTime = findViewById(R.id.cb_use_time)
        mTextContainer = findViewById(R.id.text_container)
        mNumberContainer = findViewById(R.id.number_container)
        mSelectContainer = findViewById(R.id.select_container)
        bottomSpinner = findViewById(R.id.bottomSpinner)
        upSpinner = findViewById(R.id.upSpinner)
        yaoSpinner = findViewById(R.id.yaoSpinner)
        ganZhi.text = LunarUtil.getInstance().resetNow().toString()
        val list = DataUtils.getEightDagramsModels().map {
            it.eightDiagramsName
        }.toList()
        upSpinner.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, android.R.id.text1, list)
        upSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                mUpIndex = position + 1
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }
        bottomSpinner.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, android.R.id.text1, list)
        bottomSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                mBottomIndex = position + 1
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }
        yaoSpinner.adapter = ArrayAdapter(
            this, android.R.layout.simple_spinner_item, android.R.id.text1, arrayListOf(
                "上爻",
                "五爻",
                "四爻",
                "三爻",
                "二爻",
                "初爻",
            )
        )
        yaoSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                mYaoIndex = 5 - position + 1
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }
        findViewById<View>(R.id.btn).setOnClickListener {
            val manager = EightDagramsManager.getInstance()
            val params = when (mMeasureType) {
                MeasureType.NUMBER -> {
                    val topInt = top.text.toString()
                    val bottomInt = bottom.text.toString()
                    if (topInt.isEmpty() || bottomInt.isEmpty()) {
                        Toast.makeText(applicationContext, "请输入数字", Toast.LENGTH_LONG).show()
                        return@setOnClickListener
                    }
                    MeasureParams.number(topInt.toInt(), bottomInt.toInt(), mCbUseTime.isChecked)
                }
                MeasureType.TEXT -> MeasureParams.text(mEtText.text.toString())
                MeasureType.SELECT -> {
                    MeasureParams.select(mUpIndex, mBottomIndex, mYaoIndex)
                }
                else -> MeasureParams.time()
            }
            manager.startMeasure(params)
            ResultActivity.launch(this)
        }
        findViewById<RadioGroup>(R.id.gb_container).setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rb_text -> {
                    mTextContainer.visibility = View.VISIBLE
                    mNumberContainer.visibility = View.GONE
                    mSelectContainer.visibility = View.GONE
                    mMeasureType = MeasureType.TEXT
                }
                R.id.rb_number -> {
                    mTextContainer.visibility = View.GONE
                    mNumberContainer.visibility = View.VISIBLE
                    mSelectContainer.visibility = View.GONE
                    mMeasureType = MeasureType.NUMBER
                }
                R.id.rb_time -> {
                    mTextContainer.visibility = View.GONE
                    mNumberContainer.visibility = View.GONE
                    mSelectContainer.visibility = View.GONE
                    mMeasureType = MeasureType.TIME
                }
                R.id.rb_select -> {
                    mTextContainer.visibility = View.GONE
                    mNumberContainer.visibility = View.GONE
                    mSelectContainer.visibility = View.VISIBLE
                    yaoSpinner.setSelection(5)
                    mMeasureType = MeasureType.SELECT
                }
            }
        }
    }

}
